/**
 * Created by henian.xu on 2018/11/5.
 * 过滤器
 */

import Vue from 'vue';
import Moment from 'moment';
import { Comm } from '~/utils';

Vue.filter('moment', (value, format = 'YYYY-MM-DD') => {
    // YYYY-MM-DD HH:mm:ss
    if (!value) return '';
    return Moment(value).format(format);
});

Vue.filter('number', function(value, length = 2, min = true) {
    return Comm.filterNumber(value, length, min);
});

Vue.filter('price', function(value, length = 2, min = true) {
    return Comm.filterNumber(value, length, min);
});

Vue.filter('money', function(value = '') {
    value += '';
    return value.replace(/\B(?=(\d{3})+(?!\d))/g, ',') || 0;
});
Vue.filter('htmlDecode', function(value) {
    if (value === '' || value === undefined || value === null) return '';
    const node = document.createElement('div');
    node.innerHTML = value;
    return node.innerText || '';
});
Vue.filter('substringStr', function(value) {
    const maxLength = 20;
    if (value.length > maxLength) {
        return value.substring(0, maxLength) + '...';
    } else {
        return value;
    }
});
